#include "hal_data.h"
#include <stdio.h>

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER

fsp_err_t err = FSP_SUCCESS;
unsigned char send_buff[100];
volatile bool uart_send_complete_flag = false;
void user_uart_callback (uart_callback_args_t * p_args)
{
    if(p_args->event == UART_EVENT_TX_COMPLETE)
    {
        uart_send_complete_flag = true;
    }
}
#ifdef __GNUC__      //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
    err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
    if(FSP_SUCCESS != err) __BKPT();
    while(uart_send_complete_flag == false){}
    uart_send_complete_flag = false;
    return ch;
}
int _write(int fd,char *pBuffer,int size)
{
    for(int i=0;i<size;i++)
    {
        __io_putchar(*pBuffer++);
    }
    return size;
}
uint16_t adc_result_buffer[3];
/* Callback function */
void adc_callback(adc_callback_args_t *p_args)
{
    /* TODO: add your own code here */
    if( p_args->event == ADC_EVENT_SCAN_COMPLETE )
    {
        err = R_DTC_Reset( &g_transfer0_ctrl,
                           (void*)&R_ADC0->ADDR[0],     // reset source address
                           &adc_result_buffer[0],       // reset destination address
                           1 );                         // reset block size
        if( FSP_SUCCESS != err )
        {
            __BKPT(1);
        }
    }
}

void hal_entry(void)
{
    /* TODO: add your own code here */
    err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
    assert(FSP_SUCCESS == err);
    adc_status_t adc_status;
    err = R_ADC_Open(&g_adc0_ctrl, &g_adc0_cfg);
    assert(FSP_SUCCESS == err);
    err = R_ADC_StatusGet (&g_adc0_ctrl, &adc_status);
    assert(FSP_SUCCESS == err);
    err = R_ADC_ScanCfg(&g_adc0_ctrl, &g_adc0_channel_cfg);
    assert(FSP_SUCCESS == err);       // Source is first ADC result register
    g_transfer0_cfg.p_info->p_src = (void*)&R_ADC0->ADDR[0];       // Destination is results buffer
    g_transfer0_cfg.p_info->p_dest = &adc_result_buffer[0];
    /* Open the transfer instance with initial configuration. */
    err = R_DTC_Open(&g_transfer0_ctrl, &g_transfer0_cfg);
    /* Handle any errors. This function should be defined by the user. */
    assert(FSP_SUCCESS == err);
    /* Enable the DTC to handle incoming transfer requests. */
    err = R_DTC_Enable(&g_transfer0_ctrl);
    assert(FSP_SUCCESS == err);
    while(1)
    {
        /* In software trigger mode, start a scan by calling R_ADC_ScanStart(). In other modes, enable external           * triggers by calling R_ADC_ScanStart(). */
        err = R_ADC_ScanStart(&g_adc0_ctrl);
        assert(FSP_SUCCESS == err);
        printf("adc[0]_v=%f\n",(float)adc_result_buffer[0]/4095*3.3);
        printf("adc[1]_v=%f\n",(float)adc_result_buffer[1]/4095*3.3);
        printf("adc[2]_v=%f\n",(float)adc_result_buffer[2]/4095*3.3);
        R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);// NOLINT100->160
    }
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}



/*******************************************************************************************************************//**
 * This function is called at various points during the startup process.  This implementation uses the event that is
 * called right before main() to set up the pins.
 *
 * @param[in]  event    Where at in the start up process the code is currently at
 **********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
    if (BSP_WARM_START_RESET == event)
    {
#if BSP_FEATURE_FLASH_LP_VERSION != 0

        /* Enable reading from data flash. */
        R_FACI_LP->DFLCTL = 1U;

        /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
         * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
    }

    if (BSP_WARM_START_POST_C == event)
    {
        /* C runtime environment and system clocks are setup. */

        /* Configure pins. */
        R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
    }
}

#if BSP_TZ_SECURE_BUILD

BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{

}
#endif
